<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class VideoController extends Controller
{
    public function upload() {
        $allow_type=["image/jpeg","image/gif","image/png"];
        if(!in_array($_FILES['upload_file']['type'],$allow_type)){
            $msg='图片类型不符合要求';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        elseif($_FILES['upload_file']['size']>2000000){
            $msg='图片大小不符合要求';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        elseif($_FILES['upload_file']['error']!=0){
            $msg='上传出错';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        else {
            $src_image=$_FILES['upload_file']['tmp_name'];
            $size=getimagesize($_FILES['upload_file']['tmp_name']);
            $src_width=$size[0];
            $src_height=$size[1];
            $tar_width=264;
            $tar_height=155;
            $tar_thumb=imagecreatetruecolor($tar_width,$tar_height);
            if($_FILES['upload_file']['type']=='image/jpeg'){
                $src_thumb=imagecreatefromjpeg($src_image);
            }
            elseif ($_FILES['upload_file']['type']=="image/gif"){
                $src_thumb=imagecreatefromgif($src_image);
            }
            else{
                $src_thumb=imagecreatefrompng($src_image);
            }

            imagecopyresized($tar_thumb,$src_thumb,0,0,0,0, $tar_width,$tar_height,$src_width,$src_height);
            $path='./assets/images/video_upload/'.$_FILES['upload_file']['name'];
            imagejpeg($tar_thumb,$path);
            return json_encode(['success'=>true,'img_thumb'=>'/assets/images/video_upload/'.$_FILES['upload_file']['name']]);
        }
    }
    public function videolist(){
        //$oVideos=Video::all();
        $oVideos=Video::orderby('id','desc')->paginate(6);//分页
        return view('admin.video-list')->with('oVideos',$oVideos);
    }
    public function conf(){
        $now_time= time();
        $now_date= date('Y-m-d H:i:s',$now_time);
        $oVideos=Video::where('q','=',0)->orderby('begin_time','desc')->get();
        $qVideo=Video::where('begin_time', '>', $now_date)->where('q','=',1)->orderby('begin_time')->get();
        $zVideo=Video::where('begin_time', '<=', $now_date)->where('end_time', '>', $now_date)->where('q','=', 1)->orderby('begin_time')->get();
        return view('front.conf')->with('qVideo',$qVideo)->with('zVideo',$zVideo)->with('oVideos',$oVideos);
    }
    public function review(){
        //$oVideos=Video::all();
        $oVideos=Video::where('q','=','0')->orderby('id','desc')->paginate(3);
        return view('front.review')->with('oVideos',$oVideos);
    }
    public function reviewInfo($id){
        $video=Video::find($id);
        $oVideos=Video::where('q','=','0')->where('id','!=',$id)->orderby('id','desc')->get();
        return view('front.review-info')->with('video',$video)->with('oVideos',$oVideos);
    }
    public function index(){
        $now_time= time();
        $now_date= date('Y-m-d H:i:s',$now_time);
        $oVideos=Video::where('q','=',0)->orderby('begin_time','desc')->get();
        $qVideo=Video::where('begin_time', '>', $now_date)->where('q','=',1)->orderby('begin_time')->get();
        $zVideo=Video::where('begin_time', '<=', $now_date)->where('end_time', '>', $now_date)->where('q','=',1)->orderby('begin_time')->get();
        return view('front.index')->with('qVideo',$qVideo)->with('zVideo',$zVideo)->with('oVideos',$oVideos);
    }
    public function create(){
        $oExperts=Expert::orderby('id')->get();
        return view('admin.video-create')->with('oExperts',$oExperts);
    }
    public function store(Request $request){
        $aVideo=$request->input();
        if(isset($aVideo['img_thumb'])){
            $aVideo["thumb"]=$aVideo['img_thumb'];
        }
        else{
            $aVideo["thumb"]='/assets/test_images/r1.jpg';
        }
        $aVideo['content']=$request->input('description');
        $Expert=Expert::find($aVideo['doc_id'][0]);
        $aVideo['expert']=$Expert->name;
        $video=Video::create($aVideo);
        foreach ($aVideo['doc_id'] as $v){
            $video->Lecture()->attach((int)$v);
        }
        return redirect('/adm/video/video-list');
    }
    public function edit($id){
        $video=Video::find($id);
        $oExperts=Expert::orderby('id')->get();
        return view('admin.video-edit')->with('video',$video)->with('oExperts',$oExperts);
    }
    public function update(Request $request, $id){
        $video=Video::find($id);
        $aVideo=$request->input();
        if(isset($aVideo['img_thumb'])){
            $aVideo["thumb"]=$aVideo['img_thumb'];
        }
        else{
            $aVideo["thumb"]=$video->thumb;
        }
        $Expert=Expert::find($aVideo['doc_id'][0]);
        $aVideo['expert']=$Expert->name;
        $video->update($aVideo);
        foreach ($aVideo['doc_id'] as $v){
            $video->Lecture()->attach((int)$v);
        }
        return redirect('/adm/video/video-list');
    }
    public function delete($id){
        $video=Video::find($id);
        $video->delete();
        return redirect('/adm/video/video-list');
    }
}
